//
//  ListOfAlumnos_VC.m
//  MasterUabApp
//
//  Created by Alumne on 14/10/13.
//  Copyright (c) 2013 Owlab. All rights reserved.
//


//---Imports:----
#import "ListOfAlumnos_VC.h"
#import "Alumno_Cell.h"
#import "ListAsignaturas_VC.h"
#import "DetalleAlumno_VC.h"
//---------------

@interface ListOfAlumnos_VC ()

@end

@implementation ListOfAlumnos_VC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self loadAlumnos];
}

- (void) loadAlumnos
{
    if (m_AlumnoArray == nil)
    {
        m_AlumnoArray = [[AlumnoArray alloc]init:self];
    }
    [ECommon showProgressInView:self.view withText:@"LOADING..."];
    [m_AlumnoArray getAlumnos];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) syncDone:(BOOL)success object:(BaseObject*)obj
{
    if (success)
    {
        if ([obj isKindOfClass:[AlumnoArray class]])
        {
            [ECommon hideProgress];
            [_m_TableView reloadData];
        }
    }
    else
    {
        //Show error message
        if (obj!=nil && obj.msg != nil && obj.msg.length > 0)
        {
            [ECommon setProgressDetailsLabel:obj.msg];
            [ECommon setProgressErrorAndHideWithText:NSLocalizedString(@"Problem detected:", @"")];
        }
        else
        {
            [ECommon setProgressErrorAndHideWithText:NSLocalizedString(@"Connection error", @"")];
        }
    }
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(notificationCallback:)
												 name:nil
											   object:nil ];
}

- (void)viewDidDisappear:(BOOL)animated;
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:nil
                                                  object:nil];
    
    [super viewDidDisappear:animated];
}



- (void) notificationCallback:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"info_Clicked"])
    {
        m_sAlumnoIdClicked = [notification object];
        [self performSegueWithIdentifier:@"FromListAlumnosToDetallAlumno" sender:nil];
    }
    else if ([[notification name] isEqualToString:@"asignatura_Clicked"])
    {
        m_sAlumnoIdClicked = [notification object];
        [self performSegueWithIdentifier:@"FromListAlumnosToListAsignaturas" sender:nil];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"FromListAlumnosToDetallAlumno"])
    {
        ListAsignaturas_VC * vc = [segue destinationViewController];
        vc.alumnoID = m_sAlumnoIdClicked;
    }
    else if ([[segue identifier] isEqualToString:@"FromListAlumnosToListAsignaturas"])
    {
        DetalleAlumno_VC * vc = [segue destinationViewController];
        vc.alumnoID = m_sAlumnoIdClicked;
    }
}



#pragma mark - Table view data source

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 77.f;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return m_AlumnoArray.arrayAlumnos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Alumno_Cell";
    Alumno_Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    Alumno* alumno = [m_AlumnoArray.arrayAlumnos objectAtIndex:indexPath.row];
    // Configure the cell...
    if (cell == nil)
    {
        cell = [[Alumno_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    cell.m_Label_Name.text = [NSString stringWithFormat:@"%@ %@",alumno.AlumnoNombre,alumno.AlumnoApellidos];
    
    cell.m_Image_Avatar.image = [UIImage imageNamed:@"alumnos.gif"];
    if (![alumno.AlumnoFoto isEqualToString:@""])
    {
        [self.mainAppDelegate downloadImageFromUrl:alumno.AlumnoFoto forImageView:cell.m_Image_Avatar andTag:indexPath.row];
    }
    
    [cell setAlumnoID:alumno.AlumnoId];
    return cell;
}

@end
